﻿using DBAccessTestFramework.Demo.Domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DBAccessTestFramework.Demo.DataAccess
{
    public class DBPatientStorage : IPatientStorage
    {
        public bool UpdatePatient(Domain.Patient patient)
        {
            using (var db = new EMREntities())
            {
                var patientRecord = db.Patients
                    .FirstOrDefault(a => a.PatientId == patient.PatientId);

                if (patientRecord == null)
                {
                    return false;
                }

                patientRecord.Address1 = patient.Address.Address1;
                patientRecord.Address2 = patient.Address.Address2;
                patientRecord.City = patient.Address.City;
                patientRecord.State = patient.Address.State;
                patientRecord.Zip = patient.Address.Zip;

                patientRecord.DOB = patient.DOB;
                patientRecord.FirstName = patient.FirstName;
                patientRecord.LastName = patient.LastName;
                patientRecord.SSN = patient.SSN;

                db.SaveChanges();
            }

            return true;
        }

        public int AddPatient(Domain.Patient patient)
        {
            var patientRecord = new Patient();

            patientRecord.Address1 = patient.Address.Address1;
            patientRecord.Address2 = patient.Address.Address2;
            patientRecord.City = patient.Address.City;
            patientRecord.State = patient.Address.State;
            patientRecord.Zip = patient.Address.Zip;

            patientRecord.DOB = patient.DOB;
            patientRecord.FirstName = patient.FirstName;
            patientRecord.LastName = patient.LastName;
            patientRecord.SSN = patient.SSN;

            using (var db = new EMREntities())
            {
                db.Patients.Add(patientRecord);
                db.SaveChanges();
                return patientRecord.PatientId;
            }
        }

        public bool DeletePatient(int patientId)
        {
            using (var db = new EMREntities())
            {
                #region 
                //var patientRecord = db.Patients
                //    .FirstOrDefault(a => a.PatientId == patientId);

                //if (patientRecord == null)
                //{
                //    return false;
                //}

                //patientRecord.Deleted = true;
                //db.SaveChanges();
                //return true;
                #endregion

                return db.usp_DeletePatient(patientId) > 0;
            }
        }

        public IList<PatientItem> FindPatients(FindPatientFilter filter)
        {
            using (var db = new EMREntities())
            {
                var query = db.Patients.Select(a => a);
                if (filter.DOB.HasValue)
                {
                    query = query.Where(a => a.DOB == filter.DOB);
                }

                if (!string.IsNullOrWhiteSpace(filter.FirstName))
                {
                    query = query.Where(a => a.FirstName.StartsWith(filter.FirstName));
                }

                if (!string.IsNullOrWhiteSpace(filter.LastName))
                {
                    query = query.Where(a => a.LastName.StartsWith(filter.LastName));
                }

                if (!string.IsNullOrWhiteSpace(filter.SSN))
                {
                    query = query.Where(a => a.SSN == filter.SSN);
                }

                return query.Select(a => new
                {
                    a.DOB,
                    a.FirstName,
                    a.LastName,
                    a.PatientId
                })
                    .ToList()
                    .Select(a => new PatientItem
                    {
                        DOB = a.DOB,
                        FirstName = a.FirstName,
                        LastName = a.LastName,
                        PersonId = a.PatientId
                    })
                    .ToList();
            }
        }

        public Domain.Patient FindPatient(int patientId)
        {
            throw new NotImplementedException();
        }

        public void AddVitals(VitalSignHistory vitalSigns)
        {
            throw new NotImplementedException();
        }

        public IList<VitalSignHistory> FindVitals(FindPatientFilter filter)
        {
            throw new NotImplementedException();
        }
    }
}
